package com.timothy.activity;

import com.timothy.service.CommunicationService;
import com.timothy.service.CommunicationService.CommunicationBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class CommunicationActivity extends Activity{
	CommunicationService commService;
    boolean mBound = false;
    
    // UI controls
//    private Handler myRefresh = new Handler();
    private EditText messageReceive;
    private EditText messageSend;
    
    private Button getMessageBtn;
    private Button sendMessageBtn;
    
    @Override
    public void onCreate(Bundle savedInstanceState){
    	Log.i("ServiceDemo", "AndroidNetCommunicationClientActivity.onCreate()...");
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo);
        
        // message receive from server
        messageReceive = (EditText) findViewById(R.id.messageReceive);
        
        //button to receive message
        getMessageBtn = (Button) findViewById(R.id.getMessageBtn);
        getMessageBtn.setOnClickListener(new OnClickListener(){
        	  @Override
              public void onClick(View v){
        		 String message =  commService.getReceiveMessageString();
        		 messageReceive.setText(message);
              }       	
        });
 
        //message waiting to send
        messageSend = (EditText) findViewById(R.id.messageSend);
        sendMessageBtn = (Button) findViewById(R.id.sendMessageBtn);
        
        // Subscribe to handle the button click.
        sendMessageBtn.setOnClickListener(new OnClickListener(){
        	  @Override
              public void onClick(View v){
        		  //TODO send message to server
        		  Log.i("ServiceDemo", "AndroidNetCommunicationClientActivity.sendMessageBtn.setOnClickListener()...");
        		  
        		  int num = commService.getRandomNumber();
        		  Log.i("ServiceDemo", "num:"+num);
        		  
        		  String sendMessageString = messageSend.getText().toString();
        		  Log.i("ServiceDemo", "sendMessageString:"+sendMessageString);
        		  
        		  
        		  commService.onSendRequest(sendMessageString);
              }       	
        });
        
    }
    
/*    public void onButtonClick(View v) {
        if (mBound) {
            // Call a method from the LocalService.
            // However, if this call were something that might hang, then this request should
            // occur in a separate thread to avoid slowing down the activity performance.
            int num = commService.getRandomNumber();
            commService.onSendRequest(null);
            
            Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
        }
    }*/
    
    @Override
    protected void onStart() {
    	Log.i("ServiceDemo", "AndroidNetCommunicationClientActivity.onStart()...");
        super.onStart();
        // Bind to LocalService
        Intent intent = new Intent(this, CommunicationService.class);
        bindService(intent, commConnection, Context.BIND_AUTO_CREATE);
    }
    
    private ServiceConnection commConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
        	Log.i("ServiceDemo", "AndroidNetCommunicationClientActivity.ServiceConnection.onServiceConnected()...");
            // We've bound to LocalService, cast the IBinder and get LocalService instance
        	CommunicationBinder binder = (CommunicationBinder) service;
            commService = binder.getService();
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
        	Log.i("ServiceDemo", "AndroidNetCommunicationClientActivity.onServiceDisconnected()...");
            mBound = false;
        }
    };
 
    @Override
    protected void onStop() {
    	Log.i("ServiceDemo", "AndroidNetCommunicationClientActivity.onStop()...");
    	
        super.onStop();
        // Unbind from the service
        if (mBound) {
            unbindService(commConnection);
            mBound = false;
        }
    }
    
    @Override
    protected void onDestroy(){
    	Log.i("ServiceDemo", "AndroidNetCommunicationClientActivity.onDestroy()...");
        super.onDestroy();
    } 
    
}